module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('menus', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      tenant_id: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'tenants',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '租户ID，为null表示系统菜单',
      },
      parent_id: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'menus',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '父菜单ID',
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true,
        comment: '菜单名称（唯一标识）',
      },
      title: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '菜单标题（显示名称）',
      },
      type: {
        type: Sequelize.ENUM('directory', 'menu', 'button'),
        allowNull: false,
        defaultValue: 'menu',
        comment: '菜单类型',
      },
      path: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '路由路径',
      },
      component: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '组件路径',
      },
      icon: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '图标',
      },
      permission: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '权限标识',
      },
      order_num: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '排序序号',
      },
      visible: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '是否显示',
      },
      status: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '状态（启用/禁用）',
      },
      cache: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: '是否缓存',
      },
      redirect: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '重定向路径',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
      },
    });

    // 添加索引
    await queryInterface.addIndex('menus', ['tenant_id']);
    await queryInterface.addIndex('menus', ['parent_id']);
    await queryInterface.addIndex('menus', ['type']);
    await queryInterface.addIndex('menus', ['order_num']);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('menus');
  },
};

